/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.controller;

import cn.webrx.mapper.CarouselMaper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Project: ssmapp - CarouselController
 * <p>Powered by webrx On 2022-04-02 11:50:44
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@RestController
public class CarouselController {
    @Autowired
    private CarouselMaper cm;

    @PutMapping("/updateisshow")
    public Map<String, Object> updateIsShow(@RequestBody Map<String,Object> mm) {
        int v = Integer.parseInt(mm.get("v").toString());
        int id = Integer.parseInt(mm.get("id").toString());
        cm.updateIsShow(v, id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", mm.get("t").toString() + " : " + (v == 0 ? "设置隐藏了" : "设置显示了"));
        return map;
    }

    @GetMapping("/cal/add")
    public ModelAndView add(){
        ModelAndView mv = new ModelAndView("cal/add");
        return mv;
    }

    @PostMapping("/cal/save")
    public Map<String,Object> save(MultipartFile file,String title,int isshow,String link,HttpServletRequest req) throws IOException {
        //处理上传文件
        String filename = file.getOriginalFilename().toLowerCase();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date d = new Date();
        String ext = filename.substring(filename.lastIndexOf("."));
        filename = sdf.format(d)+ext;
        if(!file.isEmpty()){
            String path = req.getServletContext().getRealPath("/static/uploads/");
            File ff = new File(path);
            if(!ff.exists()) ff.mkdirs();
            file.transferTo(new File(path,filename));
        }
        //保存数据到mysql数据库
        Map<String,Object> v = new HashMap<>();
        v.put("link",link);
        v.put("title",title);
        v.put("isshow",isshow);
        v.put("src","uploads/"+filename);
        cm.save(v);

        //返回信息
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "轮播添加成功");
        return map;
    }


    @DeleteMapping("/cal/delall")
    public Map<String,Object> delall(@RequestBody List<Map<String,Object>> list,HttpServletRequest req){
        for (Map<String, Object> m : list) {
            //删除轮播数据库记录
            int id = Integer.parseInt(m.get("id").toString());
            cm.deleteById(id);

            //删除文件,轮播图片文件
            String f = req.getServletContext().getRealPath("/static/"+m.get("src").toString());
            File file = new File(f);
            if(file.exists()){
                file.delete();
            }

        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "删除成功");
        return map;
    }

    @GetMapping("/cal/edit")
    public ModelAndView edit(){
        ModelAndView mv = new ModelAndView("cal/edit");
        return mv;
    }

    @DeleteMapping("/delete")
    public Map<String, Object> delete(@RequestBody Map<String,Object> mm, HttpServletRequest req) {
        //删除轮播数据库记录
        int id = Integer.parseInt(mm.get("id").toString());
        cm.deleteById(id);

        //删除文件,轮播图片文件
        String f = req.getServletContext().getRealPath(mm.get("src").toString());
        File file = new File(f);
        if(file.exists()){
            file.delete();
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", mm.get("t").toString() + " : 删除成功！");
        return map;
    }
}
